<?php
  class User extends Admin_Controller{
      function User(){
          parent::Admin_Controller();
          $this->load->model('User_m');
      }
      function index(){
        $this->list_user();    
      }
    function list_user(){
        is_admin();
        $this->_data['page_title'] = "Liste des utilisateurs";
        $per_page=$this->uri->segment(3);
        $offset=$this->uri->segment(4);
        if($per_page==""){
            $per_page=10;
        }
        if($offset==""){
            $offset=0;
        }
        $config['base_url']=base_url().'index.php/user/list_user/'.$per_page.'/';
        $config['per_page']='10';
        $config['uri_segment']=4;
        
        $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        
        $config['total_rows']=$this->db->count_all('client');
        if($config['total_rows']==$offset) $offset=$offset-$per_page;
         $this->_data['perpage']=$config['per_page'];
         $this->_data['offset']=$offset; 
        $this->_data['query']=$this->User_m->get_all_user($config['per_page'],$offset);
        $this->pagination->initialize($config);
        $this->_data['pagination']=$this->pagination->create_links();
        $this->_data['total']=$this->db->count_all('client'); 
        $this->display_view('client/user');
    }
    function add_user(){
        is_admin();
        $this->_data['page_title'] = "Ajouter un utilisateur";
          $config = array(
            array(
            'field'   => 'nom',
            'label'   => 'Nom',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'prenom',
            'label'   => 'Prenom',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'email',
            'label'   => 'Email',
            'rules'   => 'required|valid_email'
            ),
             array(
            'field'   => 'tel',
            'label'   => 'Telephone',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'adr',
            'label'   => 'Adresse',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'pass',
            'label'   => 'Mot de passe',
            'rules'   => 'required|matches[confirm_pass]|md5'
            ),
            array(
            'field'   => 'confirm_pass',
            'label'   => 'Confirmation du mot de passe',
            'rules'   => 'required|md5'
            )
            );
         $this->form_validation->set_rules($config);
          $this->_data['erreur']=""; 
         if($this->input->post('submit')){  
                   if($this->form_validation->run()){ 
                     $dat=date('Y-m-d');
                     $data = array(
                            'nom'=>$this->input->post('nom'),
                            'prenom'=>$this->input->post('prenom'),
                            'email'=>$this->input->post('email'),
                            'tel'=>$this->input->post('tel'),
                            'adresse'=>$this->input->post('adresse'),
                            'dat'=>$dat,
                            'pass'=>$this->input->post('pass')
                            );
                            
             $query = $this->User_m->check_inscription($data);  
                     if($query->num_rows() == 0){ 
                         
              $query = $this->User_m->envoyer_inscription($data);           
                 if($query==1)    redirect(site_url('user/list_user'));    
                 else {
                      $this->_data['erreur']="Erreur lors de l'inscription !";
                     $this->display_view('client/add_user'); 
                         }
                   }else {
                        $this->_data['erreur']="Email existe !";    
                            $this->display_view('client/add_user'); 
                           }
                        
               }  else  $this->display_view('client/add_user');    
         }    else  $this->display_view('client/add_user');
    }
    
         function delete_user($id,$perpage,$offset){ 
         is_admin(); 
          $query=$this->User_m->delete_user($id);  
             if($query==1)    redirect(site_url('user/list_user'.'/'.$perpage.'/'.$offset)); 
         }
         
        function update_user($id,$perpage,$offset){
        is_admin();
        $this->_data['page_title'] = "mise à jour utilisateur";
        $user=$this->User_m->get_user($id);
        $this->_data['perpage']=$perpage;
         $this->_data['offset']=$offset; 
          $config = array(
            array(
            'field'   => 'nom',
            'label'   => 'Nom',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'prenom',
            'label'   => 'Prenom',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'tel',
            'label'   => 'Telephone',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'adr',
            'label'   => 'Adresse',
            'rules'   => 'required'
            )
            );
         $this->form_validation->set_rules($config);
          $this->_data['erreur']=""; 
          $this->_data['user']=$user; 
         if($this->input->post('submit')){  
                      if($this->form_validation->run()){ 
                     $dat=date('Y-m-d');
                     $data = array(
                            'nom'=>$this->input->post('nom'),
                            'prenom'=>$this->input->post('prenom'),
                            'tel'=>$this->input->post('tel'),
                            'adresse'=>$this->input->post('adresse')
                            );

              $query = $this->User_m->update_user($data,$id);           
                 if($query==1)    redirect(site_url('user/list_user'.'/'.$perpage.'/'.$offset));    
                 else {
                      $this->_data['erreur']="Erreur lors de l'inscription !";
                     $this->display_view('client/update_user'); 
                         }
                  
                        
               }  else  $this->display_view('client/update_user');    
         }    else  $this->display_view('client/update_user');
    }
    function detail_user($id){
       is_admin();
       $this->_data['page_title'] = "utilisateur détail";
       $this->_data['user']=$this->User_m->get_user($id);  
       $this->_data['order']=$this->User_m->get_order_user($id);
       $this->display_view('client/detail_user');      
    }
  }
?>